﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WPF_SFrost
{
    public partial class FormGroupList : Form
    {
        SqlConnection conn = new SqlConnection(SqlDbHelper.ConnectionString);
        SqlCommand cmd;
        //SqlDataAdapter sda;
        //由于ds.clear()不会用，所以暂时只声明ds，在使用时使用 ds = new dataset()初始化。
        //DataSet ds;     // = new DataSet()；如果在此处初始化ds，那么，在程序结束前，可以将多个表的数据fill到该ds中。
        public FormGroupList()
        {
            InitializeComponent();
        }

        private void FormGroupList_Load(object sender, EventArgs e)
        {
            fill();
        }

        public void fill()
        {
            string sql = "select Id,groupname,memo from contactgroup where id <> '' order by id desc";
            //SqlConnection conn = new SqlConnection(DBHelper.connString);
            //sda = new SqlDataAdapter(sql, conn);
            //ds = new DataSet();     //如果不在此处初始化DataSet，那么相当于是重复往ds中添加了多张表的数据
            //sda.Fill(ds);
            //dgvGroupList.DataSource = ds.Tables[0];     //DataGridView绑定的是table
            DataTable dt = SqlDbHelper.ExecuteDataTable(sql);
            dgvGroupList.DataSource = dt;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormGroupAdd f = new FormGroupAdd();
            f.ShowDialog();
            fill();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvGroupList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int n = 0;
            try
            {
                n = (int)dgvGroupList.CurrentRow.Cells[0].Value;
            }
            catch (Exception)
            {
                MessageBox.Show("请选择有效的行记录。");
                return;
            }
            FormGroupDetail f = new FormGroupDetail(n);
            f.ShowDialog();
            fill();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                id = (int)dgvGroupList.CurrentRow.Cells[0].Value;
            }
            catch(Exception)
            {
                MessageBox.Show("请选择有效的记录。");
                return;
            }
            DialogResult dgr = MessageBox.Show("确定要删除该记录吗？", "提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dgr == DialogResult.Yes)
            {
                //conn.Open();
                //string sql = string.Format("select count(*) from contact where groupid = '{0}'", id);
                //cmd = new SqlCommand(sql, conn);
                //int n = (int)cmd.ExecuteScalar();
                string sql = "select count(*) from contact where groupid = @groupid";
                SqlParameter[] parameter =
                {
                    new SqlParameter("groupid",SqlDbType.Int)
                };
                parameter[0].Value = id;
                int n = (int)SqlDbHelper.ExecuteScalar(sql, CommandType.Text, parameter);
                if (n > 0)
                {
                    MessageBox.Show("该分类下已存在联系人，不允许删除！");
                    conn.Close();
                    return;
                }
                //sql = string.Format("delete from contactgroup where id = '{0}'",id);
                string sqlstr = "delete from contactgroup where id = @id";
                SqlParameter[] delParameter =
                {
                    new SqlParameter("@id",SqlDbType.Int)
                };
                delParameter[0].Value = id;
                int rows = SqlDbHelper.ExecuteNonQuery(sqlstr, CommandType.Text, delParameter); 
                if(rows != 1)
                {
                    MessageBox.Show("删除失败！");
                }
                else
                {
                    MessageBox.Show("删除成功！");
                }
            }
            fill();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int n = 0;
            try
            {
                n = (int)dgvGroupList.CurrentRow.Cells[0].Value;
            }catch(Exception)
            {
                MessageBox.Show("请选择有效的行记录。");
                return;
            }
            FormGroupDetail f = new FormGroupDetail(n);
            f.ShowDialog();
            fill();
        }
    }
}
